package com.edu.cuotibook.dao;

import com.edu.cuotibook.common.IDao;
import com.edu.cuotibook.entity.pojo.ExamPaper;
import com.edu.cuotibook.entity.pojo.Examination;
import com.edu.cuotibook.entity.pojo.WrongAnswer;
import com.edu.cuotibook.service.GenerateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * create by sym  2021/3/27 14:39.
 */
@Slf4j
@Service
public class ExcelDaoService  implements IDao<Map<Integer,String>> {

    @Autowired
    private ExamPaperDaoService examPaperDaoService;

    @Autowired
    private ExaminationDaoService examinationDaoService;

    @Autowired
    private WrongAnswerDaoService wrongAnswerDaoService;

    private static Map<Integer,String> subjects = new HashMap<>();

    //将考试信息存起来
    @Override
    public void save(List<Map<Integer, String>> list) {
        if (CollectionUtils.isEmpty(list)) {
            return ;
        }

        Map<Integer, String> head = list.get(0);
        head.remove(0);
        head.remove(1);
        head.remove(2);
        subjects = head;
        list.remove(0);

        //试卷信息
        Map<Integer, String> first = list.get(0);
        ExamPaper paper = new ExamPaper();
        paper.setTile(first.get(2));
        paper.setCourse(first.get(2).substring(0,2));
        examPaperDaoService.insertOrUpdate(paper);

        //考试信息
        Examination examination = new Examination();
        examination.setClassName(first.get(0));
        examination.setTile(first.get(2));
        examination.setPaperId(paper.getId());
        examination.setStaudentNum(list.size());
        String dir = GenerateService.homedirThreadLocal.get();
        if (!StringUtils.isEmpty(dir)) {
            examination.setReportsDir(dir);
            examination.setReportDir(dir);
            examination.setExelDir(dir);
            examination.setExamDir(dir);
            examination.setReferenceDir(dir);
            examination.setWrongansDir(dir);
            examination.setReportDirThree(dir);
        }
        examinationDaoService.insertOrUpdate(examination);

        for (Map<Integer, String> map : list) {
            WrongAnswer wrongAnswer = new WrongAnswer();
            wrongAnswer.setStudentName(map.get(1));
            wrongAnswer.setExamId(examination.getId());
            wrongAnswer.setPaperId(paper.getId());

            map.remove(0);
            map.remove(1);
            map.remove(2);

            List<WrongAnswer> wrongAnswers = new ArrayList<>(map.size());
            for (Map.Entry<Integer, String> entry : map.entrySet()) {
                if (StringUtils.isEmpty(entry.getValue())) {
                    //如果单元格内的内容为空 不能插入  插件有点问题
                    continue;
                }
                WrongAnswer pojo = new WrongAnswer();
                BeanUtils.copyProperties(wrongAnswer,pojo);
                pojo.setSubjectId((entry.getKey()-2)+"");
                pojo.setSubject(subjects.get(entry.getKey()));
                pojo.setCreateTime(new Date());
                pojo.setUpdateTime(new Date());

                wrongAnswers.add(pojo);
            }
            wrongAnswerDaoService.insertList(wrongAnswers);
        }

    }

    public void deleteExam(Long id) {
        examinationDaoService.deleteById(id);
    }
}
